<template>
    <div class="app-container">
        <table-header-component v-on:create="create" v-on:getList="getList">
            <el-input v-model="searchForm.doctor_name" placeholder="请输入名字" style="width: 200px;"></el-input>
            <!--<el-select v-model="searchForm.status" placeholder="请选择状态">-->
            <!--<el-option label="全部" value=""></el-option>-->
            <!--<el-option v-for="item in options.status" :key="item.value" :label="item.text" :value="item.value"></el-option>-->
            <!--</el-select>-->
        </table-header-component>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                        <el-form-item label="医生照片" v-if="props.row.face_image">
                            <!--<el-tag>{{ props.row.tags }}</el-tag>-->
                            <img :src="'http://orrs4ojbz.bkt.clouddn.com/'+ props.row.face_image" class="avatar">
                        </el-form-item>
                        <el-form-item label="医生标签">
                            <el-tag>{{ props.row.tags }}</el-tag>
                        </el-form-item>
                        <el-form-item label="详细描述" :show-overflow-tooltip="true">
                            <span>{{ props.row.detail_adepts }}</span>
                        </el-form-item>
                        <el-form-item label="咨询量">
                            <span>{{ props.row.consult_number }}</span>
                        </el-form-item>
                        <el-form-item label="挂号量">
                            <span>{{ props.row.registration_number }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column
                    label="医生名字"
                    prop="doctor_name">
            </el-table-column>
            <el-table-column
                    label="简单描述"
                    prop="easy_adepts"
                    :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column
                    label="排序"
                    prop="order">
            </el-table-column>
            <el-table-column
                    label="排序"
                    prop="order">

            <template slot-scope="scope">
                <el-button size="small" @click="edit(scope.row.id)">编辑</el-button>
                <el-button size="small" @click="trashed(scope.row.id)">删除</el-button>
            </template>
            </el-table-column>
        </el-table>

        <pagination-component ref="pagination" v-on:getList="getList"></pagination-component>

        <!--添加弹框-->
        <el-dialog :title="formTitle" :visible.sync="formVisible" :close-on-click-modal="false" :close-on-press-escape="false">
            <el-form class="small-space" :model="form" :rules="rules" ref="form" label-position="left" label-width="100px">
                <input type="hidden" v-model="form.id">
                <el-form-item label="医生头像" prop="face_image">
                    <el-upload
                            class="avatar-uploader"
                            :action="importFileUrl"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :on-error="handleError"
                            :before-upload="beforeAvatarUpload"
                            :data="upLoadData"
                            :on-change="handleChange">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="医生名字" prop="doctor_name">
                    <el-input v-model="form.doctor_name" placeholder="请填写医生名字 如:李三"></el-input>
                </el-form-item>
                <el-form-item label="医生标签" prop="tags">
                    <el-input v-model="form.tags" placeholder="请填写医生标签 如:院长、主任"></el-input>
                </el-form-item>
                <el-form-item label="简单描述" prop="easy_adepts">
                    <el-input v-model="form.easy_adepts" placeholder="请填写简单描述 "></el-input>
                </el-form-item>
                <el-form-item label="详细描述" prop="detail_adepts">
                    <el-input v-model="form.detail_adepts" placeholder="请填写详细描述" ></el-input>
                </el-form-item>
                <el-form-item label="点赞量" prop="consult_number">
                    <el-input v-model="form.consult_number" placeholder="请填写点赞量 如:1000"></el-input>
                </el-form-item>
                <el-form-item label="问诊量" prop="registration_number">
                    <el-input v-model="form.registration_number" placeholder="请填写问诊量 如:1000"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="order">
                    <el-input v-model="form.order" placeholder="请填写排序整数 如:1"></el-input>
                </el-form-item>
            </el-form>
            <dialog-footer-component v-on:submit="submit(formName = 'form')" v-on:close="close"></dialog-footer-component>
        </el-dialog>



        <!--编辑弹框-->
        <el-dialog :title="editTitle" :visible.sync="editVisible" :close-on-click-modal="false" :close-on-press-escape="false">
            <el-form class="small-space" :model="editform" :rules="rules" ref="editform" label-position="left" label-width="100px">
                <input type="hidden" v-model="editform.id">
                <el-form-item label="医生头像" prop="face_image">
                    <el-upload
                            class="avatar-uploader"
                            :action="importFileUrl"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess2"
                            :on-error="handleError"
                            :before-upload="beforeAvatarUpload"
                            :data="upLoadData"
                            :on-change="handleChange">
                        <img v-if="editform.face_image" :src="editimageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="医生名字" prop="doctor_name">
                    <el-input v-model="editform.doctor_name" placeholder="请填写医生名字 如:李三"></el-input>
                </el-form-item>
                <el-form-item label="医生标签" prop="tags">
                    <el-input v-model="editform.tags" placeholder="请填写医生标签 如:院长、主任"></el-input>
                </el-form-item>
                <el-form-item label="简单描述" prop="easy_adepts">
                    <el-input v-model="editform.easy_adepts" placeholder="请填写简单描述 "></el-input>
                </el-form-item>
                <el-form-item label="详细描述" prop="detail_adepts">
                    <el-input v-model="editform.detail_adepts" placeholder="请填写详细描述" ></el-input>
                </el-form-item>
                <el-form-item label="点赞量" prop="consult_number">
                    <el-input v-model="editform.consult_number" placeholder="请填写点赞量 如:1000"></el-input>
                </el-form-item>
                <el-form-item label="问诊量" prop="registration_number">
                    <el-input v-model="editform.registration_number" placeholder="请填写问诊量 如:1000"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="order">
                    <el-input v-model="editform.order" placeholder="请填写排序整数 如:1"></el-input>
                </el-form-item>
            </el-form>
            <dialog-footer-component v-on:submit="editsubmit(formName = 'editform')" v-on:close="editclose"></dialog-footer-component>
        </el-dialog>




    </div>
</template>
<style>
    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
</style>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
<script>
    import PaginationComponent from '../../common/pagination-component.vue';
    import TableHeaderComponent from '../../common/table-header-component.vue';
    import DialogFooterComponent from '../../common/dialog-footer-component.vue';
    //import { quillEditor } from 'vue-quill-editor';

    export default {
        components: {
            'pagination-component': PaginationComponent,
            'table-header-component': TableHeaderComponent,
            'dialog-footer-component': DialogFooterComponent,
            // 'quill-edit': quillEditor,
        },
        data() {
            return {
                formTitle: '',
                editTitle: '',
                dialogVisible:false,
                restrictTwo:true,
                importFileUrl: 'http://upload-z2.qiniup.com',
                imageUrl:'',
                editimageUrl:'',
                upLoadData: {
                    token: '',
                },
                formVisible:false,
                editVisible:false,
                tableData: [],
                editData: [],
                searchForm: {
                    doctor_name:'',
                    subject_id:'',
                },
                form: {
                    subject_id: '',
                    face_image:'',
                    doctor_name: '',
                    tags: '',
                    easy_adepts: '',
                    detail_adepts: '',
                    consult_number: '',
                    registration_number: '',
                    order: '',
                },
                editform: {
                    id:'',
                    subject_id: '',
                    face_image:'',
                    doctor_name: '',
                    tags: '',
                    easy_adepts: '',
                    detail_adepts: '',
                    consult_number: '',
                    registration_number: '',
                    order: '',
                },
                options: {
                    status: [],
                    categories: []
                }
            }
        },
        mounted() {
            this.getList();
        },
        methods: {
            getList() {
                let _this = this;
                let search = {
                    //'doctor_name': ['like',_this.searchForm.doctor_name],
                    'doctor_name': _this.searchForm.doctor_name,
                    'subject_id': this.$route.params.id
                };
                let paramsData = { 'data': { 'search': search } };
                axios.get('/backend/doctor?page=' + _this.$refs.pagination.pageData.current_page, { params: paramsData }).then(response => {
                    let { status, data, message } = response.data;
                    _this.tableData = data.lists.data;
                    _this.$refs.pagination.pageData.per_page = parseInt(data.lists.per_page);
                    _this.$refs.pagination.pageData.current_page = parseInt(data.lists.current_page);
                    _this.$refs.pagination.pageData.total = parseInt(data.lists.total);
                })
            },
            getUptoken() {
                let _this = this;
                axios.get('api/qiniu/token').then(response => {
                    let { status, data, message } = response.data;
                    _this.upLoadData.token= data.uptoken;
                });
            },
            submit(formName) {
                let _this = this;
                _this.form.subject_id=this.$route.params.id;
                _this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('/backend/doctor/create', { 'data': _this.form }).then(response => {
                            let {status, data, message} = response.data;
                            if (!status) {
                                _this.$message.error(message);
                                return false;
                            }
                            _this.$message.success(message);
                            //_this.$router.push({ path: '/brand/comment' });
                            _this.formVisible = false;
                            this.form.face_image='';
                            this.getList();
                        });
                    }
                });
            },
            handleChange(file,fileList) {
                //限制数量
                if(fileList.length>2){
                    fileList.splice(0,1);
                    this.restrictTwo=false;
                }
            },
            handleAvatarSuccess(res, file) {
                //上传成功后在图片框显示图片
                this.imageUrl ='http://orrs4ojbz.bkt.clouddn.com/'+ res.key;
                this.form.face_image=res.key;
                this.dialogVisible=true;
            },
            handleAvatarSuccess2(res, file) {
                //上传成功后在图片框显示图片
                this.editimageUrl ='http://orrs4ojbz.bkt.clouddn.com/'+ res.key;
                this.editform.face_image=res.key;
                this.editVisible=true;
            },

            create() {
                let _this = this;
                _this.formTitle = '添加科目';
                Vue.resetForm(_this.form);
                _this.formVisible = true;
                _this.getUptoken();
            },
            details(){

            },
            close(){
                let _this = this;
                _this.formVisible = false;
                this.form.subject_image='';
            },
            edit(id){
                //编辑
                this.getUptoken();
                let _this = this;
                let OneSearch = {
                    'id': id,
                };
                let paramsData = { 'data': { 'search': OneSearch } };
                axios.get('/backend/doctor', { params: paramsData }).then(response => {
                    let { status, data, message } = response.data;
                    _this.editVisible = true;
                    this.editimageUrl ='http://orrs4ojbz.bkt.clouddn.com/'+ data.lists.data[0].face_image;
                    _this.editform.id = id;
                    _this.editform.face_image = data.lists.data[0].face_image;
                    _this.editform.doctor_name = data.lists.data[0].doctor_name;
                    _this.editform.tags = data.lists.data[0].tags;
                    _this.editform.easy_adepts = data.lists.data[0].easy_adepts;
                    _this.editform.detail_adepts = data.lists.data[0].detail_adepts;
                    _this.editform.consult_number = data.lists.data[0].consult_number;
                    _this.editform.registration_number = data.lists[0].data.registration_number;
                    _this.editform.order = data.lists[0].data.order;
                })

            },
            editsubmit(formName) {
                let _this = this;
                _this.editform.subject_id=this.$route.params.id;
                _this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('/backend/doctor/updates', { 'data': _this.editform }).then(response => {
                            let {status, data, message} = response.data;
                            if (!status) {
                                _this.$message.error(message);
                                return false;
                            }
                            _this.$message.success(message);
                            _this.editVisible = false;
                            this.editform.face_image='';
                            this.getList();
                        });
                    }
                });
            },
            editclose(){
                let _this = this;
                _this.editVisible = false;
                this.editform.subject_image='';
            },
            trashed(id){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post('/backend/doctor/destroy', { 'data': id }).then(response => {
                        let {status, data, message} = response.data;
                        if (!status) {
                            _this.$message.error(message);
                            return false;
                        }
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

        }
    }
</script>